﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
using System.Windows.Data;
using SMS.Entities.BaseClasses;
using SMS.Model;
using System.Collections.ObjectModel;
using SMS.Entities.Admin;
using System.Collections.Generic;
using SMS.Common.Silverlight;
using System.ComponentModel;

namespace SMS.ViewModel.Admin
{
    public class ViewModel_UpdateUser:BaseNotify
    {
        AuthenticationModel _model;
        public ViewModel_UpdateUser()
        {
            SearchCommand = new DelegateCommand(Search_Click, null);
            SaveCommand = new DelegateCommand(Save_Click, null);            
            CancelCommand = new DelegateCommand(Cancel, null);
            if (!DesignerProperties.IsInDesignTool)
            {
                _model = new Model.AuthenticationModel();
         
                _model.SaveNewUserCompleted += new Action<ApplicationUser>(_model_SaveNewUserCompleted);
                _model.GetApplicationUserListCompleted += new Action<ObservableCollection<ApplicationUser>>(_model_GetApplicationUserListCompleted);
            }
        }

        void _model_GetApplicationUserListCompleted(ObservableCollection<ApplicationUser> obj)
        {

            UserMessage = obj.Count + " User found";
            UsersList = new List<Entities.Admin.ApplicationUser>(obj);
            PagedUsersList = new PagedCollectionView(UsersList);
        }

        void _model_SaveNewUserCompleted(ApplicationUser obj)
        {
            UserMessage = "User saved successfully.";
            SelectedMaster.Login_ID = obj.Login_ID;
        }
                
        void _model_OnServiceError(Entities.SMSException obj)
        {
            ErrorMessage = obj.Message;
        }

        private List<Entities.Admin.ApplicationUser> _UsersList;

        public List<Entities.Admin.ApplicationUser> UsersList
        {
            get { return _UsersList; }
            set { _UsersList = value; OnPropertyChanged("UsersList"); }
        }

        private Entities.Admin.ApplicationUser _SelectedMaster;

        public Entities.Admin.ApplicationUser SelectedMaster
        {
            get { return _SelectedMaster; }
            set { _SelectedMaster = value; OnPropertyChanged("SelectedMaster"); }
        }

        private PagedCollectionView _PagedUsersList;

        public PagedCollectionView PagedUsersList
        {
            get { return _PagedUsersList; }
            set { _PagedUsersList = value; OnPropertyChanged("PagedUsersList"); }
        }

        private string _SearchText;

        public string SearchText
        {
            get { return _SearchText; }
            set { _SearchText = value; OnPropertyChanged("SearchText"); }
        }

        private string _Text;

        public string Text
        {
            get { return _Text; }
            set { _Text = value; OnPropertyChanged("Text"); }
        }


        public ICommand SearchCommand { get; private set; }

        public void Search_Click(object param)
        {
            _model.GetApplicationUserList(App.LoggedinUserName,SearchText);

            UserMessage = "Searching users...";
        }

        public ICommand SaveCommand { get; set; }

        public void Save_Click(object param)
        {
            if (SelectedMaster != null)
            {
                _model.SaveNewUser(App.LoggedinUserName ,SelectedMaster);

                UserMessage = "Saving user...";
            }
            else
            {
                ErrorMessage = "No user to save";
            }
        }
        
        public ICommand CancelCommand { get; set; }
        public void Cancel(object param)
        {
            App.GoBack();
        }
    }
}
